/*
 * ImageMagic v0.6
 *
 * Copyright 2012 http://www.sinreweb.com/im/
 * Includes toolkit lib
 * Date: 2011-6-14
 */


/*
 *	功能：核心组件
 *	作用：实现软件初始化
 */

! function(window, undefined) {

	var doc,toolkit,root,rt,IM;

	IM=imageMagic

	IM.tk.addEvent(document,'DOMContentLoaded',function() {
		doc = document;


		//启用浏览器功能测试
		try {
			var temp = doc.createElement('canvas'),cxt = temp.getContext('2d');
		} catch(e) {
			doc.body.innerHTML = '';
			alert('改浏览器不支持canvas API,请使用firefox3.6+，IE8+，chrome2.0+,Opera10+浏览器。');
		}

		IM.root = doc.getElementsByTagName('body')[0];
		//IM.tab(imageMagic.config.workSpace.tab);

		//启用功能菜单
		IM.accordion({wrapper:imageMagic.config.workSpace.control.id,current:'accordionHandler'}).init();

		//启用调整工作区功能
		IM.workSpace.workSpaceControl();//调整工作区
		IM.workSpace.workInit();
		/////////////////////////////////////////

		//启用图片列表组件
		IM.workSpace.imageLoader();


		/////////////////////////////////////////
		//初始化功能
		IM.workSpace.init();
		IM.workSpace.destroy();

		IM.workSpace.canvasControl();


		IM.module.filter.init();
		/////////////////////////////////////////

		/////////////////////////////////////////
		//装载水印
		//IM._mark.getMark();
		/////////////////////////////////////////

		/////////////////////////////////////////
		//生成自定义剪裁框尺寸
		//IM._clip.updataSelect()
		/////////////////////////////////////////

		/*IM.tk.addEvent(window, 'beforeunload', function() {
		 localStorage.clear();
		 }, false);*/


		/*\\IM.tk.addEvent(IM.clip.area,'dblclick',function(){
		 IM._clip.getImage();
		 },false);*/
		IM.tk.addEvent(window, 'resize', function() {
			IM.workSpace.workSpaceControl();
			//IM.config.imageLoader.loader && IM.workSpace.imageLoader();
			IM.workSpace.init();
		}, false);
		 images = document.getElementById("imageList").getElementsByTagName("img"),
         i = images.length;
		 IMC = IM.config;
		 IM.images || (IM.images = images);
         IM.tk.each(images,
         function(a, b) {
             void
             function(i) {
                 IM.tk.addEvent(b, "dblclick",
                 function(e) {
                     IM.currentEdit = i;
                     imageInstall(IM,b.src,
                     function() {
                         var temp = IMC.imageLoader.loader;
                         temp.style.display = "none"
                     });
                     IM.tk.stopEvent(e)
                 },
                 false)
             } (a)
         })
	});
}(window);

function imageInstall(IM,imgSrc, fn) {
    var canvas = IM.canvas,
    cxt = canvas.getContext("2d");
    IM.img = new Image;
    IM.sourceImg = "";
    tk = IM.tk;
    tk.addEvent(IM.img, "load",
    function() {
        var canvasSize = [IM.img.width, IM.img.height];
        IM.maxSize = canvasSize;
        cxt.strokeStyle = cxt.createPattern(IM.img, "no-repeat");
        canvas.width = canvasSize[0],
        canvas.height = canvasSize[1];
        cxt.drawImage(IM.img, 0, 0, canvasSize[0], canvasSize[1]);
        typeof(fn) === "function" && fn()
    },
    false);
  
    tk.addEvent(IM.img, "error",
    function() {
        alert("图片读取失败，请使用Ctrl+F5")
    },
    false);
    IM.sourceImg = imgSrc;
    IM.img.src = imgSrc;
    document.getElementById(IMC.canvasContainer).appendChild(canvas);
}

